package com.ylp.starter;

import com.ylp.beans.BeanFactory;
import com.ylp.core.ClassScanner;
import com.ylp.web.handler.HandlerManager;
import com.ylp.web.server.TomcatServer;
import org.apache.catalina.LifecycleException;

import java.io.IOException;
import java.util.List;

/**
 * @description:
 * @date: 2019/9/13 17:44.
 * @author: yulipeng.
 */
public class ApplicationMain {

    public static void run(Class<?> cls, String[] args){
        System.out.println("framework-starter1");

        TomcatServer tomcatServer = new TomcatServer(args);
        try {
            tomcatServer.startServer();

            // 扫描包下的所有类，并返回类
            List<Class<?>> classList = ClassScanner.scanClass(cls.getPackage().getName());

            // 初始化所有的Bean
            BeanFactory.initBean(classList);

            // 扫描所有的controller并放入List集合中
            HandlerManager.resolveMappingHandler(classList);

            classList.forEach(item -> System.out.println(item.getName()));
        } catch (LifecycleException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
